package sadi.client;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.Border;


public class RegisterClient extends JDialog {

	JButton registerButton, clearButton, cancelButton;
	JPanel infoPanel, inputPanel, buttonPanel;
	JLabel usernameLabel, passwordLabel, confirmPasswordLabel;
	JTextField userNameTA;
	JPasswordField passwordTA, confirmPasswordTA;
	
	public RegisterClient(JFrame parent, String title)
	{
		super(parent, title);
		
		//Initialize all the button
		registerButton = new JButton("Register");
		clearButton = new JButton("Clear");
		cancelButton = new JButton("Cancel");
		//cancelButton.addActionListener((ActionListener) this);
		
		//add to button panel
		buttonPanel = new JPanel(new FlowLayout());
		buttonPanel.add(registerButton);
		buttonPanel.add(clearButton);
		buttonPanel.add(cancelButton);
		
		//Initialize all the labels
		usernameLabel = new JLabel("UserName: ");
		passwordLabel = new JLabel("Password: ");
		confirmPasswordLabel = new JLabel("Confirm Password: ");
		infoPanel = new JPanel(new GridLayout(3,0)); 
		
		//add the label to panel
		infoPanel.add(usernameLabel);
		infoPanel.add(passwordLabel);
		infoPanel.add(confirmPasswordLabel);
		
		//Initialize all the JTextArea
		userNameTA = new JTextField(20);
		passwordTA = new JPasswordField(20);
		confirmPasswordTA = new JPasswordField(20);
		
		//add the textareas to the panel
		inputPanel = new JPanel(new GridLayout(3,0));
		inputPanel.add(userNameTA);
		inputPanel.add(passwordTA);
		inputPanel.add(confirmPasswordTA);
		
		//adding each jpanel to the container
		Container contentPane = getContentPane();
		contentPane.setLayout(new BorderLayout());
		contentPane.add(infoPanel, BorderLayout.LINE_START);
		contentPane.add(inputPanel, BorderLayout.LINE_END);
		contentPane.add(buttonPanel, BorderLayout.PAGE_END);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
	    pack(); 
	    setVisible(true);
	}
	
	//The Action Listener part
	
	public void addCancelRegistrationListener(ActionListener lsn)
	{
		cancelButton.addActionListener(lsn);
	}
 
	public void addClearFormListener(ActionListener lsn)
	{
		clearButton.addActionListener(lsn);
	}
	
	public void addNewUserListener(ActionListener lsn)
	{
		registerButton.addActionListener(lsn);
	}
	
	//get the first password input by user
	public char[] getPassword1()
	{
		return passwordTA.getPassword();
	}
	
	//get the confirmation password
	public char[] getPassword2()
	{
		return confirmPasswordTA.getPassword();
	}
	
	//resetting both the password fields
	public void resetPasswordField()
	{
		passwordTA.setText("");
		confirmPasswordTA.setText("");
	}
	
	//reset all the fields
	public void resetAllFields()
	{
		userNameTA.setText("");
		passwordTA.setText("");
		confirmPasswordTA.setText("");
	}
	
	//return the username entered by user
	public String getUsername()
	{
		return userNameTA.getText();
	}
  

}
